#pragma once
#include <iostream>
using namespace std;
#include <functional>
#include <ctime>
#include <vector>
#include <unistd.h>
using task_t = function<void()>;

class TaskManager
{
public:
    TaskManager()
    {
        srand(time(nullptr));
	tasks.push_back([]()
              { std::cout << "sub process[" << getpid() << " ] 执行访问数据库的任务\n"
                                    << std::endl; });
	tasks.push_back([]()
              { std::cout << "sub process[" << getpid() << " ] 执行url解析\n"
                                    << std::endl; });
	tasks.push_back([]()
              { std::cout << "sub process[" << getpid() << " ] 执行加密任务\n"
                                    << std::endl; });
	tasks.push_back([]()
              { std::cout << "sub process[" << getpid() << " ] 执行数据持久化任务\n"
                                    << std::endl; });
    }
    int SelectTask()
    {
        return rand()%tasks.size();
    }
    void Execute(unsigned long number)
    {
        if(number>tasks.size()||number<0)
            return;
        tasks[number]();
    }
    ~TaskManager()
    {}
private:
    vector<task_t> tasks;
};

TaskManager _tm;

void Worker()
{
    while(true)
    {
        int cmd =0;
        int n = read(0,&cmd,sizeof(cmd));
        if(n==sizeof(cmd))
        {
            _tm.Execute(cmd);
        }
        else if(n==0)
        {
            std::cout<<"pid: "<<getpid()<<" quit..."<<std::endl;
            break;
        }
        else
        {

        }
        
    }
}